/*
 * 文件名： UserManagerImpl.java
 * 
 * 创建日期： 2010-3-11
 *
 * Copyright(C) 2010, chengguangming.
 *
 * 原始作者: <a href="mailto:chengguangming@fetionyy.com.cn">chengguangming</a>
 *
 */
package com.richdown.movie.manager;

import java.util.Iterator;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.feinno.pay.model.FetionRep;
import com.feinno.pay.model.FetionReq;
import com.feinno.pay.model.LevelRep;
import com.feinno.pay.model.UserInfo;
import com.feinno.pay.model.UserInfoRep;
import com.feinno.pay.model.UserRep;
import com.feinno.pay.model.UserReq;
import com.feinno.pay.model.VipRep;
import com.feinno.pay.util.LevelConvertUtil;
import com.feinno.pay.util.LoadConfig;
import com.feinno.xframe.codec.Base64;
import com.feinno.xframe.util.LogUtils;

@Service("userManager")
public class UserManagerImpl implements UserManager {

	private Logger logger = Logger.getLogger(UserManagerImpl.class);

	@Autowired
	private ServerClientManager serverClient;
	@Autowired
	private LoadConfig loadConfig;
	/**
	 * 获取飞信用户信息
	 * @throws Exception 
	 */
	@Override
	public UserInfo getFetionUserInfo(UserInfo userInfo) throws  Exception {
		FetionRep fetionRep = null;
//		FetionReq fetionReq = new FetionReq();
		// 设置默认名字
		userInfo.setUserName("飞信用户");
		userInfo.setUserArea("");
		userInfo.setCarrierId("0");
		// 取用户信息
//		fetionReq.setFunction("getUserInfoByUid");
//		UserReq userReq = new UserReq();
//		userReq.setUid(userInfo.getUid());
//		fetionReq.setBody(userReq);
		String content="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><fetion><function>getUserInfoByUid</function><body><uid>"+userInfo.getUid()+"</uid></body></fetion>";
		try {
			fetionRep = (FetionRep) serverClient.sendMessage(content, loadConfig.getOxpUrl(), "UserInfo");
		} catch (Exception e1) {
			logger.error(e1.getMessage());
		}
		if (fetionRep != null) {
			UserRep userRep = (UserRep) fetionRep.getBody().get(0);
			logger.debug("飞信号：" + userRep.getSid() + "，手机号：" + userRep.getMobileNo());

			Vector<?> v2 = userRep.getUserInfo();
			String car = userRep.getCarrierId();
			if (StringUtils.isNotBlank(car))
				userInfo.setCarrierId(car);
			userInfo.setMoble(userRep.getMobileNo());
			userInfo.setEmail(userRep.getEmail());
			userInfo.setSid(userRep.getSid());

			Iterator<?> it = v2.iterator();
			while (it.hasNext()) {
				UserInfoRep userInfoRep = (UserInfoRep) it.next();
				userInfo.setUserName(new String(Base64.decode(userInfoRep.getNickname()),"UTF-8"));
				if (StringUtils.isNotBlank(userInfoRep.getRegion()))
					userInfo.setUserArea(userInfoRep.getRegion());
			}

		}
		return userInfo;
	}
	/**
     * 取用户是否会员
     */
    @Override
    public UserInfo getIsVip(UserInfo userInfo) {
        FetionRep fetionRep = null;
        FetionReq fetionReq = new FetionReq();
        // 默认非会员
        userInfo.setIsVip("0");

        // 到IBMP判断是否会员
        String content="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><fetion><function>checkExtSvc</function><body><uid>"+userInfo.getUid()+"</uid><svcId>50</svcId></body></fetion>";
        try {
            fetionRep = serverClient.sendMessage(content,loadConfig.getOxpUrl(), "VipResponse");
        } catch (Exception e) {
            logger.error(LogUtils.getExceptionTrace(e));
        }
        if (fetionRep != null) {
            VipRep vipRep = (VipRep) fetionRep.getBody().get(0);
            if (StringUtils.isBlank(vipRep.getResult())
                    || !"1".equals(vipRep.getResult())) {
                userInfo.setIsVip("0");
            } else {
                userInfo.setIsVip(vipRep.getResult());
            }
        }
//    	userInfo.setIsVip("0");
        return userInfo;
    }

    /**
     * 取用户等级
     */
    public UserInfo getLevel(UserInfo userInfo) {
        FetionRep fetionRep = null;
        userInfo.setLevel(LevelConvertUtil.convert("-1"));
        String content="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><fetion><function>getLevel</function><body><uid>"+userInfo.getUid()+"</uid></body></fetion>";
        try {
            fetionRep = (FetionRep) serverClient.sendMessage(content,loadConfig.getSsUrl(), "Level");
        } catch (Exception e) {
            logger.error(LogUtils.getExceptionTrace(e));
        }
        if (fetionRep != null) {
            LevelRep levelRep = (LevelRep) fetionRep.getBody().get(0);
            if (StringUtils.isBlank(levelRep.getLevel())
                    || "-99".equals(levelRep.getLevel())
                    || "-1".equals(levelRep.getLevel())) {
                userInfo.setLevel(LevelConvertUtil.convert("-1"));
            } else {
                userInfo.setLevel(LevelConvertUtil.convert(levelRep.getLevel()));
            }
        } else {
            userInfo.setLevel(LevelConvertUtil.convert("-1"));
        }
//    	userInfo.setLevel("0001");
        return userInfo;
    }
public static void main(String[] args) {
	System.out.println(UserManagerImpl.class.getName());
}
}
